package org.droidfly;

import java.io.IOException;

import org.droidfly.NavigationDataService.NavigationDataServiceBinder;
import org.droidfly.ServoControlService1.ServoControlServiceBinder;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.PowerManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class SensorsActivity extends Activity  {

	// Debugging
	private static final String TAG = "SensorsActivity";
	private static final boolean D = true;

	private SeekBar skbAzimuth;
	private SeekBar skbPitch;
	private SeekBar skbRoll;

	private TextView txvAzimuth;
	private TextView txvRoll;
	private TextView txvPitch;

	private TextView txvStartPosition;
	private TextView txvCurrentPosition;
	private TextView txvDesinationPosition;
	private TextView txvDistance;
	private TextView txvBearing;
	private TextView txvTrack;
	private TextView txvDesiredTrack;

	private Button btnPinStartLocation;
	private Button btnPinDestinationLocation;

	private int SEEK_BAR_MAX = 300;

	private PowerManager pm ;
	private PowerManager.WakeLock wl;
	
	@Override
	public void onConfigurationChanged(
			android.content.res.Configuration newConfig) {
		super.onConfigurationChanged(newConfig);

	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (D)
			Log.e(TAG, "+++ ON CREATE +++");

		// Set up the window layout
		setContentView(R.layout.sensors_test);

		skbAzimuth = (SeekBar) findViewById(R.id.skbAzimuth);
		skbPitch = (SeekBar) findViewById(R.id.skbPitch);
		skbRoll = (SeekBar) findViewById(R.id.skbRoll);
		
		skbPitch.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				
				
				
			
				
			}
		});
		
		

		skbAzimuth.setMax(SEEK_BAR_MAX * 2);
		skbPitch.setMax(SEEK_BAR_MAX /2);
		skbRoll.setMax(SEEK_BAR_MAX);

		txvAzimuth = (TextView) findViewById(R.id.txvAzimuth);
		txvPitch = (TextView) findViewById(R.id.txvPitch);
		txvRoll = (TextView) findViewById(R.id.txvRoll);	
		

		txvStartPosition = (TextView) findViewById(R.id.txvStartPosition);
		txvCurrentPosition = (TextView) findViewById(R.id.txvCurrentPosition);
		txvDesinationPosition = (TextView) findViewById(R.id.txvDestinationPosition);
		txvDistance = (TextView) findViewById(R.id.txvDistance);
		txvBearing = (TextView) findViewById(R.id.txvBearing);
		txvTrack = (TextView) findViewById(R.id.txvTrack);
		txvDesiredTrack = (TextView) findViewById(R.id.txvDesiredTrack);

		txvStartPosition.setText("No pinned position");
		txvBearing.setText("No pinned position");
		txvDistance.setText("No pinned position");

		btnPinStartLocation = (Button) findViewById(R.id.btnPinStartLocation);
		btnPinStartLocation.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Location currentLocation = navigationDataService
						.getCurrentLocation();
				if (null != currentLocation)
					navigationDataService.setStartLocation(currentLocation);
			}
		});

		btnPinDestinationLocation = (Button) findViewById(R.id.btnPinDestinationLocation);
		btnPinDestinationLocation.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Location currentLocation = navigationDataService
						.getCurrentLocation();
				if (null != currentLocation)
					navigationDataService
							.setDestinationLocation(currentLocation);
			}
		});
		
		pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
		wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "We need sensors");
		wl.acquire();
	}

	@Override
	protected void onResume() {
		if (false == isNavigationDataServiceBound) {
			Intent intent = new Intent(this, NavigationDataService.class);
			bindService(intent, navigationDataServiceConnection,
					Service.BIND_AUTO_CREATE);
		}		
		super.onResume();
	}

	protected void onDestroy() {
		if (isNavigationDataServiceBound) {
			unbindService(navigationDataServiceConnection);
			isNavigationDataServiceBound = false;
		}
		wl.release();
		super.onDestroy();
	};

	// The Handler that gets information back from the BluetoothChatService
	private final Handler ndMsgHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			int progresValue = 0;
			switch (msg.what) {
			case NavigationDataService.MSG_AZIMUTH_CHANGED:
				float azimuth = navigationDataService.getAzimuth();
				progresValue = (int) ((azimuth + 3) * 100);
				skbAzimuth.setProgress(progresValue);
				txvAzimuth.setText(progresValue + "");

				break;

			case NavigationDataService.MSG_PICH_CHANGED:
				int pitch = navigationDataService.getPitch();
				
				progresValue = pitch + 90;
				

				
				
				skbPitch.setProgress(progresValue);
				txvPitch.setText(progresValue + "");
				
				break;

			case NavigationDataService.MSG_ROLL_CHANGED:
				float roll = navigationDataService.getRoll();
				progresValue = (int) ((roll + 1.5) * 100);
				skbRoll.setProgress(progresValue);
				txvRoll.setText(progresValue + "");
				
				break;

			case NavigationDataService.MSG_LOCATION_CHANGED:

				Location currentLocation = navigationDataService
						.getCurrentLocation();
				double altitude = currentLocation.getAltitude();
				double latitude = currentLocation.getLatitude();
				double longitude = currentLocation.getLongitude();
				txvCurrentPosition.setText(latitude + ":" + longitude + ":"
						+ altitude);

				Location startLocation = navigationDataService
						.getStartLocation();
				altitude = startLocation.getAltitude();
				latitude = startLocation.getLatitude();
				longitude = startLocation.getLongitude();
				txvStartPosition.setText(latitude + ":" + longitude + ":"
						+ altitude);

				Location destinationLocationLocation = navigationDataService
						.getDestinationLocation();
				altitude = destinationLocationLocation.getAltitude();
				latitude = destinationLocationLocation.getLatitude();
				longitude = destinationLocationLocation.getLongitude();
				txvDesinationPosition.setText(latitude + ":" + longitude + ":"
						+ altitude);

				double distance = navigationDataService
						.getDistanceToDestination();
				double track = navigationDataService.getTrack();
				double desiredTrack = navigationDataService.getDesiredTrack();
				double bearing = navigationDataService.getBearing();

				txvDistance.setText(distance + "m");
				txvBearing.setText(bearing + "�");
				txvDesiredTrack.setText(desiredTrack + "�");
				txvTrack.setText(track + "�");
				break;

			// case MESSAGE_TOAST:
			// Toast.makeText(getApplicationContext(),
			// msg.getData().getString(TOAST), Toast.LENGTH_SHORT)
			// .show();
			// break;
			}
		}
	};

	// / SERVICE

	private boolean isNavigationDataServiceBound = false;
	private NavigationDataService navigationDataService = null;
	/** Defines callbacks for service binding, passed to bindService() */
	private ServiceConnection navigationDataServiceConnection = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName className, IBinder service) {
			// We've bound to LocalService, cast the IBinder and get
			// LocalService instance
			NavigationDataServiceBinder binder = (NavigationDataServiceBinder) service;
			navigationDataService = binder.getService();
			navigationDataService.setHandler(ndMsgHandler);
			isNavigationDataServiceBound = true;
		}

		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			isNavigationDataServiceBound = false;
		}
	};

	
}
